package com.quan.system.web.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.quan.common.base.bean.ResultBean;
import com.quan.common.biz.support.BaseController;
import com.quan.common.utils.JwtTokenUtils;
import com.quan.common.utils.RedisOperator;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;

@RestController
@RequestMapping("/system/online")
public class SystemOnlineUser extends BaseController {

	@Autowired
	private RedisOperator redisOperator;
	
	/**
	 * JwtToken秘钥
	 */
	@Value("${jwt.secret:123456}")
	private String jwtSecret;
	
	/**
	 * 从Redis中获取登录用户
	 * @return
	 */
	@GetMapping(value = "/users")
	public Object getListOnlineUser() {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>() ;
		
		Set<String> keys = this.redisOperator.keys("sso:token:*");
		for (String key : keys) {
			
			String onlineUser = this.redisOperator.get(key) ;
			Jws<Claims> parse = JwtTokenUtils.parse(onlineUser, jwtSecret);
			Claims body = parse.getBody() ;
			
			Map<String, Object> map = new HashMap<String, Object>() ;
			map.put("userId", body.get("userId"));
			map.put("account", body.get("account"));
			map.put("realName", body.get("realName"));
			list.add(map) ;
		}
		
		return ResultBean.ok(list) ;
	}
	
}
